Desktop.RSSWindow = function (_parent, _iconPath, _appName) {
    Desktop.Window.call(this, _parent, _iconPath, _appName);
}

Desktop.RSSWindow.prototype = Desktop.Window.subclassOf(Desktop.Window);

Desktop.RSSWindow.prototype.createWindowContent = function (_windowReference) {
    // Set the default rss url.
    var url = 'http://feeds.arstechnica.com/arstechnica/index?format=rss';

    // Get the container for the loader.
    var ajaxContainer = $(_windowReference).find('.ajaxLoaderContainer')[0];

    // Create the ajax object.
    var ajax = new Desktop.AjaxCon();

    // Get an XHR object.
    var xhr = ajax.getXHR();

    // Set the onreadystatechange function.
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                // Get the contentContainer.
                var content = $(_windowReference).find('.windowContentContainer')[0];

                // Create a container for the rss.
                var rssContainer = document.createElement('div');
                rssContainer.className = 'rssContainer';

                rssContainer = Desktop.RSSWindow.prototype.styleFeed(rssContainer, xhr.responseText);

                // Add the container to the content.
                $(content).append(rssContainer);

                // Add the updated notification.
                Desktop.RSSWindow.prototype.createBottomBarContent($(_windowReference).find('.bottomBar')[0], true);
            }
            else {
                // Give error.
                $(ajaxContainer).addClass('hideLoader');
            }
        }
    }

    // Open the xhr.
    xhr.open('get', 'backend/rss.php?url=' + escape(url), true);

    // Send the request.
    xhr.send(null);
}

Desktop.RSSWindow.prototype.createBottomBarContent = function (bottomBar, loaded) {
    if (loaded == null || loaded == undefined || loaded == false) {
        // Create the container.
        var ajaxContainer = document.createElement('div');
        ajaxContainer.className = 'ajaxContainer';

        // Create the loader.
        var loader = document.createElement('img');
        loader.src = 'resources/ajaxLoader/ajax-loader-white.gif';
        loader.className = 'ajaxLoader';

        // Create the text.
        var textContainer = document.createElement('div');
        textContainer.textContent = 'Loading...';
        textContainer.className = 'ajaxText';

        // Add the loader and text to the container.
        ajaxContainer.appendChild(loader);
        ajaxContainer.appendChild(textContainer);

        // Add the loader to the bottomBar.
        bottomBar.appendChild(ajaxContainer);
    }
    else {
        if (ajaxContainer == null || ajaxContainer == undefined) {
            ajaxContainer = $(bottomBar).find('.ajaxContainer')[0];
        }
        bottomBar.removeChild(ajaxContainer);

        // Create the container.
        ajaxContainer = document.createElement('div');
        ajaxContainer.className = 'ajaxContainer';

        // Get the date and time.
        var d = new Date();
        var time = undefined;
        if (d.getMinutes() < 10) {
            time = d.getHours() + ":0" + d.getMinutes();
        }
        else {
            time = d.getHours() + ":" + d.getMinutes();
        }
        // Create the text.
        textContainer = document.createElement('div');
        textContainer.textContent = 'Last update: ' + time;
        textContainer.className = 'ajaxText';

        // Add the loader and text to the container.
        ajaxContainer.appendChild(textContainer);

        // Add the loader to the bottomBar.
        bottomBar.appendChild(ajaxContainer);
    }
}

Desktop.RSSWindow.prototype.styleFeed = function (rssContainer, feedInput) {
    // Split up the article at each h2 tag.
    var articles = feedInput.split('<h2', feedInput.length);

    // Go throught the array and add back the h2 tag.
    for (var i = 1; i < articles.length; i++) {
        articles[i] = '<h2' + articles[i];
    }

    // Go through each article and add a new div to them.
    for (var x = 1; x < articles.length; x++) {
        articles[x] = '<div class="rssArticleContainer"><div class="article">' + articles[x] + '</div></div>';
    }

    // Recounstruct the string.
    var newFeed = '';
    for (var y = 0; y < articles.length; y++) {
        newFeed += articles[y];
    }

    // Add the string to the container.
    $(rssContainer).append(newFeed);

    // Get a collection of the elements rather than the strings.
    var articleCollection = $(rssContainer).find('.rssArticleContainer');

    for (var z = 0; z < articleCollection.length; z++) {
        // For each element make even numbers one color and leave odd numbers as base color.
        if (z % 2 == 0) {
            $(articleCollection[z]).addClass('odd');
        }
        else {
            $(articleCollection[z]).addClass('even');
        }
    }

    return rssContainer;
}